<?php


class ColumnModel extends Model{

	protected $tableName = 'column';


    public function getMid($id){
      $map['cid'] = $id;
      return $this->where($map)->getField('mid');
    }
    // 获取 栏目分级信息
    public function getcolumn($pid = 0){
        $map['pid'] = $pid;
        $list = $this->where($map)->order('sort asc')->select();
        foreach($list as $k => $v){
            $html.='<li class="dd-item"><div class="dd-handle"> <a style="line-height: 22px;" href="'.U('Column/arclist',array('cid'=>$v['cid'])).'">'.'id：'.$v['cid'].'--'.$v['title'].'</a><div class="pull-right columnrow ">';
            $html.='<input type="text" class="inputsort pull-right gui-input" value="'.$v['sort'].'" key="'.$v['cid'].'-" >';
            $html.='<a href="'.U('Admin/Column/delColumn', array('id'=>$v['cid'])).'" class=" pull-right btn btn-xs btn-danger ">删除</a> ';
            $html.='<a href="'.U('Admin/Column/editColumn',array('id'=>$v['cid'])).'" class="pull-right btn btn-xs btn-primary ">编辑</a> ';
            $html.='<a href="'.U('Admin/Column/addColumn', array('pid'=>$v['cid'])).'" class="pull-right btn btn-xs btn-dark ">新增子类</a> ';
            $html.='</div></div>';
            $column[$v['cid']] = $v;
            $map['pid'] = $v['cid'];
            $isnext = D('column')->where($map)->count();
            if($isnext){
                $html .='<ol class="dd-list">';
                $nexthtml = $this->getcolumn($v['cid']);
                $html .= $nexthtml.'</ol></li>';
            }else{
                $html .='</li>';
            }
        }
        return $html;
    }

	  public function anliselect($selected = 0,$pid = 0 ,$num = 1){
        $map['pid']  = $pid;
		$map['mid']  = 3;
        $html = '';
        if($num > 1){
            for($is = 1 ; $is < $num ; $is++){
                 $header .='&nbsp;&nbsp;';
            }
        }
        $list = $this->where($map)->order('sort asc')->select();
        foreach($list as $k=> $v){
            $xz = ($v['cid'] == $selected)?'selected':'';
            $html .='<option '.$xz.' value='.$v['cid'].'>'.$header.$v['title'].'</option>';
            $map2['pid'] = $v['cid'];
            $isnext = D('column')->where($map)->count();
            if($isnext){
                $num++;
                $nexthtml = $this->anliselect($selected,$v['cid'],$num);
                $num-- ;
                $html .= $nexthtml;
            }
        }
        return $html;
    }


       public function getcolumn2($pid = 0,$lv = 1){

        $map['pid'] = $pid;
        $modelname  = D('Model')->modelname();

        $list = $this->where($map)->order('sort asc')->select();

//         var_dump($list);
//         exit();


        foreach($list as $k => $v){

            $title_header = '<span class="columnnav-'.$lv.' columnpid-'.$v['cid'].'"></span>';
            $v['title'] = $title_header . $v['title'];
            $html.='<tr class="message-unread">
                          <td class="text-center hidden-xs">
                            <label class="option block mn">
                              <input class="arcinput" type="checkbox" name="mobileos" data-id="'.$v['cid'].'">
                              <span class="checkbox mn"></span>
                            </label>
                          </td>
                          <!--<td class="text-center">'.$v['cid'].'</td>-->
                          <td class="">
                          <a class="column_a" href="'.U('Admin/Column/arclist', array('cid'=>$v['cid'])).'">'.$v['title'].'</a></td>
                          <td class="text-center">'.$modelname[$v['mid']].'</td>
                          <td class="text-center">
                            <!--
                              <div class="btn-group">
                                <a href="'.U('Admin/Column/addColumn', array('pid'=>$v['cid'])).'" class=" btn btn-xs btn-success ">新增</a>
                                <a href="'.U('Admin/Column/delColumn', array('id'=>$v['cid'])).'" class="  btn btn-xs btn-danger ">删除</a>
                                <a href="'.U('Admin/Column/editColumn',array('id'=>$v['cid'])).'" class=" btn btn-xs btn-primary ">编辑</a>
                                <a href="'.U('Admin/Column/arclist', array('cid'=>$v['cid'])).'" class=" btn btn-xs btn-dark ">列表</a>
                              </div>
                            -->
                          </td>
                          <td class="text-center"><input type="text" class="inputsort gui-input" style="width:40px;height:22px;text-align:center;" value="'.$v['sort'].'" key="'.$v['cid'].'-" ></td>
                    </tr>';
            $column[$v['cid']] = $v;
            $map['pid'] = $v['cid'];
            $isnext = D('column')->where($map)->count();
            if($isnext){
                $lv++;
                $html .= $this->getcolumn2($v['cid'],$lv);
                $lv--;
            }
        }
        return $html;
    }


    public function getDelcolumn( $id = 0  ){
        $ids = $this->getGroupcolumn( $id );
        $arr = explode(',', $ids);
        return array_filter($arr);
    }


    // 获取需要的栏目及其下属栏目id
    public function getGroupcolumn( $id ){
        $map['pid']  = $id;
        $ids =$id.',';
        $isnext = $this->where($map)->count();
        if($isnext){
            $list = $this->where($map)->select();
            foreach($list as $k => $v){
                $ids .= $this->getGroupcolumn($v['cid']);
            }
        }
        return $ids;
    }


    // 获取分类列表 select形式展示
    public function getSelectcolumn($selected = 0,$pid = 0 ,$num = 1){
        $map['pid']  = $pid;
        $html = '';
        if($num > 1){
            for($is = 1 ; $is < $num ; $is++){
                 $header .='&nbsp;&nbsp;';
            }
        }
        $list = $this->where($map)->order('sort asc')->select();
        foreach($list as $k=> $v){
            $xz = ($v['cid'] == $selected)?'selected':'';
            $html .='<option '.$xz.' value='.$v['cid'].'>'.$header.$v['title'].'</option>';
            $map2['pid'] = $v['cid'];
            $isnext = D('column')->where($map)->count();
            if($isnext){
                $num++;
                $nexthtml = $this->getSelectcolumn($selected,$v['cid'],$num);
                $num-- ;
                $html .= $nexthtml;
            }
        }
        return $html;
    }

    public function getUrl($info){
        if(is_numeric($info)){
            $info = $this->where('cid = '.$info)->find();
        }
        if($info['url']){
            $url = $info['url'];
        }else if($info['mid']){
            $url = U($info['application'].'/'.$info['controller'].'/'.$info['function']);
        }else{
            $url = U($info['application'].'/'.$info['controller'].'/'.$info['function'],array('cid'=>$info['cid']));
        }
        return $url;
    }

		

}

?>
